#ifndef WXSCODER_H
#define WXSCODER_H

#include <M_Code_editor.h>
#include <wx/string.h>
#include <wx/timer.h>


class wxsCoder: public wxEvtHandler {
  public:

    wxsCoder();
    virtual ~wxsCoder();

    void AddCode(
      const wxString& FileName,
      const wxString& Header,
      const wxString& End,
      const wxString& Code,
      bool Immediately = true,
      bool CodeHasHeader = false,
      bool CodeHasEnd = false );

    wxString GetCode( const wxString& FileName, const wxString& Header,
                      const wxString& End,
                      bool IncludeHeader = false,
                      bool IncludeEnd = false );

    wxString GetFullCode( const wxString& FileName, wxFontEncoding& Encoding, bool &UseBOM );
    void PutFullCode( const wxString& FileName, const wxString& Code, wxFontEncoding Encoding, bool UseBOM );
    void Flush( int Delay );
    static wxsCoder* Get() { return Singleton; }

  private:
    struct CodeChange {
      wxString Header;
      wxString End;
      wxString Code;
      bool CodeHasHeader;
      bool CodeHasEnd;
      CodeChange* Next;
    };

    WX_DEFINE_ARRAY( CodeChange*, CodeChangeArray );
    wxMutex DataMutex;
    wxTimer FlushTimer;
    CodeChangeArray CodeChanges;
    wxArrayString CodeChangesFiles;

    bool ApplyChangesEditor(
      M_Code_Editor* Editor,
      const wxString& Header,
      const wxString& End,
      wxString& Code,
      bool CodeHasHeader,
      bool CodeHasEnd,
      wxString& EOL );
    bool ApplyChangesString(
      wxString& Content,
      const wxString& Header,
      const wxString& Env,
      wxString& Code,
      bool CodeHasHeader,
      bool CodeHasEnd,
      bool& HasChanged,
      wxString& EOL );

    void FlushFile( const wxString& FileName );
    void FlushAll();
    void FlushTimerEvent( wxTimerEvent& event );
    wxString RebuildCode( wxString& BaseIndentation, const wxChar* Code, int CodeLen, wxString& EOL );
    wxString CutSpaces( wxString Code, int Count );
    static wxString NormalizeFileName( const wxString& FileName );
    static wxsCoder* Singleton;
};

#endif
